package com.inspector.app.util;

import android.util.Log;


import com.inspector.app.BuildConfig;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author zlq
 * @date 2019/6/6
 */
public class LogUtil {
    static String className;//类名
    static String methodName;//方法名
    static int lineNumber;//行数
    static StringBuffer buffer = new StringBuffer();

    /**
     * 判断是否可以调试
     *
     * @return
     */
    public static boolean isLogable() {
        return BuildConfig.DEBUG;
    }

    private static String createLog(String log) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("===");
        stringBuffer.append(methodName);
        stringBuffer.append("(").append(className).append(":").append(lineNumber).append(")===:");
        stringBuffer.append(log);
        buffer.append(stringBuffer);
        return stringBuffer.toString();
    }

    /**
     * 获取文件名、方法名、所在行数
     *
     * @param sElements
     */
    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    public static void e(String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(message));
    }

    public static void e(String TAG, String message) {
        if (!isLogable()) {
            return;
        }
        Log.e(TAG, "" + message);
    }

    public static void i(String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(message));
    }

    public static void i(String TAG, String message) {
        if (!isLogable()) {
            return;
        }
        Log.i(TAG, "" + message);
    }

    public static void d(String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, createLog(message));
    }

    public static void d(String TAG, String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.d(TAG, createLog(message));
    }

    public static void v(String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(message));
    }

    public static void v(String TAG, String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.v(TAG, createLog(message));
    }

    public static void w(String message) {
        if (!isLogable()) {
            return;
        }
        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(message));
    }

    public static void printToTxt() {
        String log = new String(buffer);
        File file = new File(ConstantUtil.BASE_DIR + "/log");
        if (!file.exists()) {
            file.mkdirs();
            LogUtil.e(file.exists() + "File>>>isExists");
        }
        try {
            File fileOut = new File(file.getAbsolutePath() + "/log_" + System.currentTimeMillis() + ".txt");
            LogUtil.e(fileOut.exists() + "TXT>>>isExists");
            FileWriter fileWriter = new FileWriter(fileOut);
            fileWriter.write(log);
            fileWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        buffer = new StringBuffer();
    }
}
